package hd.wenda.unzip;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import net.lingala.zip4j.exception.ZipException;

import java.io.File;

import hd.wenda.c.unzip.CompressStatus;
import hd.wenda.c.unzip.ZipUtil;

public class MainActivity extends AppCompatActivity {
    private Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case CompressStatus.START:
                    mTextview.setText("开始解压!");
                    break;
                case CompressStatus.HANDLING:
                    Bundle b = msg.getData();
                    mTextview.setText(b.getInt(CompressStatus.PERCENT)+" ");
                    break;
                case CompressStatus.COMPLETED:
                    mTextview.setText("解压完成!");
                    break;
                case CompressStatus.ERROR:
                    mTextview.setText("解压完成");
                    break;
            }
        };
    };


    private TextView mTextview;
    String zipFilePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextview = (TextView) findViewById(R.id.textview);

        zipFilePath = Environment.getExternalStorageDirectory().toString() + "/CHINESE.zip";

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File zipFile = new File(zipFilePath);
                try
                {
                    ZipUtil.unZipFileWithProgress(zipFile, Environment.getExternalStorageDirectory() + "/myTest/", handler,
                            true);
                }
                catch (ZipException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }




}
